var config = require('../config')
var constant = require('./constant')

const LOGIN_URL = `${config.BASE_PATH}/login`;//登录服务
const FULL_USER_INFO_URL = `${config.BASE_PATH}/decodeUserInfo`;//解密微信用户信息并保存在服务端
const CHECK_LOGIN_URL = `${config.BASE_PATH}/checkLogin`;//校验是否登录

/**
 * 校验登录
 */
var checkLogin = function(success, fail) {
    var loginToken = wx.getStorageSync(constant.LOGIN_TOKEN);
    if (!loginToken) {
        typeof fail == "function" && fail();
    } else {
        //先请求微信判断用户是否在登录状态
        wx.checkSession({
            success: function () {
                //然后在判断本地服务器该用户的登录状态
                wx.request({
                    url: CHECK_LOGIN_URL,
                    header: {
                        'loginToken': loginToken
                    },
                    complete: function (res) {
                        if(res.statusCode=='200'&&res.data.flag==0){
                            typeof success == "function" && success();
                        }else {
                            typeof fail == "function" && fail();
                        }
                    }
                });
            },
            fail: function () {
                typeof fail == "function" && fail();
            }
        })
    }
}

/**
 * 登录
 */
var login = function(success, fail) {
    checkLogin(function(){
        typeof success == "function" && success();
    },function(){
        remoteLogin(success, fail)
    });
}

/**
 * 服务端请求登录
 */
var remoteLogin = function(success, fail) {
    //调用登录接口
    wx.login({
        success: function (loginRes) {
            wx.request({
                url: LOGIN_URL,
                data: {
                    code: loginRes.code
                },
                complete: function (res) {
                    if (res.statusCode == '200'&&res.data.flag==0) {
                      wx.setStorageSync(constant.LOGIN_TOKEN,res.data.message);
                      typeof success == "function" && success();
                    } else {
                        if (typeof fail == "function") {
                            fail();
                        } else {
                            wx.showModal({
                                title: '温馨提示',
                                content: '服务器异常！',
                                showCancel: false,
                                complete: function (res) {
                                  if (res.confirm) {
                                    wx.redirectTo({
                                      url: '/pages/404/404',
                                    });
                                  }
                                }
                            });
                        }
                    }
                }
            })
        },
        fail : function(res){
            wx.showModal({
                title: '温馨提示',
                content: '服务器异常！',
                showCancel: false,
                complete: function (res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '/pages/404/404',
                    });
                  }
                }
            });
        }
    })
}

module.exports = {
    login: login,
    checkLogin: checkLogin
}